package com.zhiyun.yycall.ui;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.gzit.utils.AndroidUtils;
import com.gzit.utils.StringUtils;
import com.zhiyun.message.ActiveRequest;
import com.zhiyun.message.LoginResponse;
import com.zhiyun.message.SendActivateSMSRequest;
import com.zhiyun.message.SendActivateSMSResponse;
import com.zhiyun.net.AsynCallback;
import com.zhiyun.net.Net;
import com.zhiyun.net.Response;
import com.zhiyun.trace.TraceActivity;
import com.zhiyun.yycall.Config;
import com.zhiyun.yycall.R;

public class ActivateActivity extends TraceActivity {

	private String phoneNo;

	private OnClickListener listener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.active_btn:
				remoteActivate();
				break;
			case R.id.send_activie_code_btn:
				remoteResentActivateSMS();
				break;

			}

		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.active_activity);
		this.findViewById(R.id.active_btn).setOnClickListener(listener);
		this.findViewById(R.id.send_activie_code_btn).setOnClickListener(
				listener);
		phoneNo = this.getIntent().getStringExtra("phoneNo");
		((TextView) this.findViewById(R.id.phoneNo)).setText(phoneNo);

	}

	private void remoteResentActivateSMS() {

		SendActivateSMSRequest req = new SendActivateSMSRequest();
		req.setPhoneNo(phoneNo);
		req.setPassword(Config.getPassword());

		Net.doHttpRemote(this, req, new SendActivateSMSResponse(),
				new AsynCallback<Response>() {

					@Override
					public boolean callback(Response r) {
						if (r.isFail()) {
							
							switch(r.getErrorCode())
							{
							 case Response.ACTIVATE_CODE_REQUEST_FREQUENCY:
								 AndroidUtils.toastShort("短信验证码已发送，如未收到，请1分钟后请求新的验证码");
								break;
							default:
								AndroidUtils.toastShort("短信激活码重发失败，原因："
										+ r.getError());
								break;
							}
						
							return true;
						}
						AndroidUtils.toastShort("短信激活码发送成功，请注意接收云之呼的激活码短信");

						return true;

					}
				});

	}

	private void remoteActivate() {

		EditText code = (EditText) this.findViewById(R.id.activie_code);
		if (StringUtils.isBlank(code.getText().toString())) {
			AndroidUtils.toastLong("激活码不能为空");
			return;

		}

		ActiveRequest req = new ActiveRequest();
		req.setPhoneNo(phoneNo);
		req.setActiveCode(code.getText().toString());

		Net.doHttpRemote(this, req, new LoginResponse(),
				new AsynCallback<Response>() {

					@Override
					public boolean callback(Response r) {
						if (r.isFail()) {
							AndroidUtils.toastShort("登录失败,原因：" + r.getError());
							return true;
						}

						setResult(Activity.RESULT_OK);
						finish();
						return true;

					}
				});

	}
}
